/*
* AUTHOR: Kevin Lam
*/
package com.apps.ubc.cc.controller;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.apps.datastore.UBCCourseSpiderDatastore;
import com.apps.datastore.dao.BookInformation;
import com.apps.datastore.dao.CourseInformationObject;
import com.apps.datastore.dao.SectionInformationObject;
import com.apps.datastore.dao.UniqueCourseObject;
import com.apps.services.AmazonWebService;
import com.apps.services.SaveonbookService;
import com.apps.services.UBCWayfindingService;
import com.apps.services.UBCSectionDetailService;
import com.apps.services.factory.AmazonWebServiceFactory;
import com.apps.services.factory.SaveonBookServiceFactory;
import com.apps.services.factory.UBCWayfindingServiceFactory;
import com.apps.ubc.cc.model.AmazonBookModel;
import com.apps.ubc.cc.model.BuildingModel;
import com.apps.ubc.cc.model.SectionModel;
public class DetailsController extends HttpServlet {
private static final String DEPARTMENT_TYPE = "department";
private static final String COURSE_TYPE = "course";
private static final String SECTION_TYPE = "section";
// public void doGet(HttpServletRequest req, HttpServletResponse resp){
// try {
// req.getRequestDispatcher("/search.jsp").forward(req, resp);
// } catch (ServletException e) {
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
public void doGet(HttpServletRequest req, HttpServletResponse resp) {
try {
String type = req.getParameter("type");
if (type.equals(DEPARTMENT_TYPE)) {
//String dID = req.getParameter("did");
} else if (type.equals(COURSE_TYPE)) {
String dID = req.getParameter("did");
String cID = req.getParameter("cid");
resp.sendRedirect("/search?q=" + dID + "%20"
+ cID);
} else if (type.equals(SECTION_TYPE)) {
String dID = req.getParameter("did");
String cID = req.getParameter("cid");
String sID = req.getParameter("sid");
SectionModel sm = getSectionModel(dID, cID, sID);
HttpSession s = req.getSession();
s.setAttribute("section", sm);
try {
req.getRequestDispatcher("/details.jsp").forward(req, resp);
} catch (ServletException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private SectionModel getSectionModel(String d, String c, String s){
UBCCourseSpiderDatastore sd = new UBCCourseSpiderDatastore();
SectionInformationObject sio = sd.querySectionFromId(d, c, s);
CourseInformationObject cio = sd.queryCourseFromId(d, c);
UBCWayfindingService uws = UBCWayfindingServiceFactory.getUBCWayfindingService();
BuildingModel blo = uws.search(sio.getBuilding());
String lat = "";
String lng = "";
if (blo != null) {
lat = blo.getLatitude();
lng = blo.getLongitude();
}
String dept = sio.getDepartmentId();
String course = sio.getCourseId();
String section = sio.getSectionId();
String day = sio.getDay();
String term = sio.getTerm();
String building = sio.getBuilding();
String room = sio.getRoom();
String activity = sio.getActivity();
String title = cio.getCourseTitle();
String instructor = sio.getInstructor();
String drop = sio.getDropNoWDate();
String withdraw = sio.getDropWDate();
String start = sio.getStart();
String cdf = "No";
if(sio.isCdf())
cdf = "Yes";
String end = sio.getEnd();
String location = sio.getLocation();
SectionModel sm = new SectionModel( dept, course, section,
activity, term, day, location,
start, end, instructor, building,
room, cdf, drop, withdraw,
title, lat, lng);
return sm;
}
public String getSectionInformation(String dID, String cID, String sID){
UBCCourseSpiderDatastore sd = new UBCCourseSpiderDatastore();
UBCWayfindingService uws = UBCWayfindingServiceFactory.getUBCWayfindingService();
SectionInformationObject sio = sd.querySectionFromId(dID, cID, sID);
String out = "";
String dept = sio.getDepartmentId();
String course = sio.getCourseId();
String section = sio.getSectionId();
String day = sio.getDay();
String term = sio.getTerm();
BuildingModel blo = uws.search(sio.getBuilding());
UBCSectionDetailService w = new UBCSectionDetailService();
w.initContent(new UniqueCourseObject(dept,course,section));
List<BookInformation> biol = w.getBookList();
out += "<table>";
out += "<tr>\n";
out += "<td>"+dept+" "+course+" "+section+"</td>\n";
out += "</tr>\n";
out += "<tr>\n";
out += "<td><b>Activity: </b>"+sio.getActivity()+"</td>\n";
out += "<td><b>Term: </b>"+term+"</td>\n";
out += "</tr>\n";
out += "<tr>\n";
out += "<td><b>Day: </b>"+day+"</td>\n";
out += "<td><b>Time: </b>"+sio.getStart()+" - "+sio.getEnd()+"</td>\n";
out += "</tr>\n";
if(!sio.getInstructor().isEmpty()){
out += "<tr>\n";
out += "<td><b>Instructor: </b>"+sio.getInstructor()+"</td>\n";
out += "</tr>\n";
}
if(blo != null) {
out += "<tr>\n";
out += "<td><b>Building: </b>"+sio.getBuilding()+"</td>\n";
out += "<td><b>Room: </b>"+sio.getRoom()+"</td>\n";
if(blo.getLatitude() != "" && blo.getLongitude() != "")
out += "<td>"+"<img src=\"http://maps.googleapis.com/maps/api/staticmap?center="+blo.getLatitude()+","+blo.getLongitude()+"&zoom=17&size=320x320&markers=color:blue%7Clabel:S%7C"+blo.getLatitude()+","+blo.getLongitude()+"&maptype=hybrid&sensor=false\" width=\"320\" height=\"320\">"+"</td>";
out += "</tr>\n";
}
if(!biol.isEmpty()){
out += "<tr>\n";
out += "Books: \n";
out += "</tr>\n";
}
for(Iterator<BookInformation> i = biol.iterator(); i.hasNext();){
BookInformation bio = i.next();
SaveonbookService sbss = SaveonBookServiceFactory.getSaveonbookService();
String optimizedTitle = sbss.getOptimizedTitle(bio);
String searchTitle = optimizedTitle.replaceAll(" ", "%20");
out += "<tr>\n";
out += "<table>\n";
out += "<tr>\n";
out += "<a href=\"http://saveonbook.com/search/title-search.jsp?title="+searchTitle+"&active=0\">SaveOnBook</a>";
out += "</tr>\n";
out += "<tr>\n";
out += "<td><b>Title: </b>"+bio.getTitle()+" - "+bio.getRequired()+"</td>\n";
int pr[] = sbss.getPriceRange(sbss.titleSearch(optimizedTitle));
out += "<td><b>Price Range </b>: $"+pr[0]+" - $"+pr[1]+"</td>\n";
out += "</tr>\n";
out += "</table>\n";
out += "</tr>\n";
}
for(Iterator<BookInformation> i = biol.iterator(); i.hasNext();){
BookInformation bio = i.next();
AmazonWebService aws = AmazonWebServiceFactory.getAmazonWebService();
AmazonBookModel abm = aws.search(bio.getISBN());
out += "<tr>\n";
out += "<table>\n";
if (!abm.getAsin().equals("")) {
out += "<tr>\n";
out += "<a href=\"" + abm.getDetailUrl() + "\">Amazon</a>";
out += "</tr>\n";
out += "<tr>\n";
out += "<td><img src=\"" + abm.getImageUrl() + "\"></td>\n";
out += "<td><b>Title: </b>" + abm.getTitle() + " - "
+ bio.getRequired() + "</td>\n";
out += "</tr>\n";
out += "<tr>\n";
out += "<td><b>Price(New): </b>" + abm.getPriceNew()
+ "</td>\n";
out += "</tr>\n";
out += "<tr>\n";
out += "<td><b>Price(Used): </b>" + abm.getPriceUsed()
+ "</td>\n";
out += "</tr>\n";
}
out += "</table>\n";
out += "</tr>\n";
}
out += "</table>";
out += "<br>\n";
return out;
}
}